import { message } from "ant-design-vue";
import axios from "axios";
import type {
    CreateAxiosDefaults,
    AxiosInstance,
    InternalAxiosRequestConfig,
    AxiosResponse,
    AxiosRequestConfig,
} from "axios";
import { getToken } from "@/utils/cookie";
type ResponseDataDefault = { code: number; data: any } & Record<string, any>;
class HTTP<T = unknown, D extends ResponseDataDefault = ResponseDataDefault> {
    public config: CreateAxiosDefaults<T> | undefined;
    public install: AxiosInstance;
    constructor(config: CreateAxiosDefaults<T> | undefined) {
        this.config = config;
        this.install = axios.create(this.config);
        this.init();
    }
    // 统一请求
    public request<P, R = D>(config: AxiosRequestConfig<P> = {}): Promise<AxiosResponse<R, P>> {
        return this.install(config);
    }
    // 添加拦截器
    protected init(): void {
        this.install.interceptors.request.use(
            (config: InternalAxiosRequestConfig<T>) => {
                if (config.url?.indexOf(":9200") != -1) {
                    config.auth = { username: "elastic", password: "qqqAAA0130" };
                }
                if (config.url !== "/login" && !getToken()) {
                    import("@/utils/common").then((res) => {
                        res.backLogin();
                        message.error("登录失效,请重新登录!");
                    });
                }
                if (!config.url?.includes("8080") && !config.url?.includes("https")) {
                    config["headers"]["Token"] = getToken();
                }
                return config;
            },
            (err) => {
                return Promise.reject(err);
            }
        );
        this.install.interceptors.response.use(
            // @ts-ignore
            async (response: AxiosResponse<D, T>) => {
                if (response.status === 200) {
                    if (String(response.data.code).startsWith("2")) {
                        return Promise.resolve(response);
                    } else if (String(response.data.code).startsWith("3")) {
                        return Promise.resolve(response);
                    } else if (String(response.data.code).startsWith("4")) {
                        if (response.data.code === 403) {
                            await import("@/utils/common").then((res) => {
                                res.backLogin();
                                message.error("登录失效,请重新登录!");
                            });
                            return Promise.reject(response);
                        }
                        return Promise.resolve(response);
                    } else if (String(response.data.code).startsWith("5")) {
                        return Promise.reject(response);
                    }
                }
                return Promise.resolve(response);
            },
            (err) => {
                console.log("服务器错误!");
                return Promise.reject(err);
            }
        );
    }
    public post<R = D, P = Record<string, any>>(
        url: string,
        data?: P,
        headers?: Record<string, any>,
        config?: AxiosRequestConfig
    ) {
        return this.request<P, R>({
            url,
            method: "POST",
            data,
            headers,
            ...config
        });
    }
    public put<R = D, P = Record<string, any>>(
        url: string,
        data?: P,
        headers?: Record<string, any>,
        config?: AxiosRequestConfig
    ) {
        return this.request<P, R>({
            url,
            data,
            method: "PUT",
            headers,
            ...config
        });
    }

    public delete<R = D, P = Record<string, any>>(
        url: string,
        data?: P,
        headers?: Record<string, any>,
        config?: AxiosRequestConfig
    ) {
        return this.request<P, R>({
            url,
            data,
            method: "DELETE",
            headers,
            ...config
        });
    }
    public patch<R = D, P = Record<string, any>>(
        url: string,
        data?: P,
        headers?: Record<string, any>,
        config?: AxiosRequestConfig
    ) {
        return this.request<P, R>({
            url,
            data,
            method: "PATCH",
            headers,
            ...config
        });
    }
    public get<R = D, P = Record<string, any>>(
        url: string,
        params?: P,
        headers?: Record<string, any>,
        config?: AxiosRequestConfig
    ) {
        return this.request<P, R>({
            url,
            params,
            method: "GET",
            headers,
            ...config
        });
    }
}
export default HTTP;
